#include "head.h"
int makeWorker(threadPool_t *pThreadPool){
    for(int i=0; i<pThreadPool->threadNum; ++i){
        pthread_create(pThreadPool->tidArr+i, NULL, threadFunc, (void *)pThreadPool);
    }
}
// 根据用户的一整条命令buf 分析出命令的种类，存到command里
int getCommand(char buf[], char command[]){
    for(int i = 0; i < strlen(buf); ++i){
        if(buf[i] == ' ')
            break;
        command[i] = buf[i];
    }
}
void unlock(void* parg){
    pthread_mutex_t *pMutex = (pthread_mutex_t *)parg;
    pthread_mutex_unlock(pMutex);
}
void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg;
    while(1){
        int netFd;
        char curPath[200];
        pthread_mutex_lock(&pThreadPool->mutex);
        pthread_cleanup_push(unlock, &pThreadPool->mutex);
        while(pThreadPool->userQueue.size == 0 && pThreadPool->isThreadPoolRunning == 1){
            pthread_cond_wait(&pThreadPool->cond, &pThreadPool->mutex);
        }
        if(pThreadPool->isThreadPoolRunning == 0){
            puts("child quit");
            pthread_exit(NULL);
        }
        
        puts("GetTask");
        netFd = pThreadPool->userQueue.pFront->netFd;
        strcpy(curPath, pThreadPool->userQueue.pFront->curPath);
        deUserQueue(&pThreadPool->userQueue);
        pthread_cleanup_pop(1);
        printf("netfd = %d, curPath = %s\n", netFd, curPath);
        
        handleEvent(netFd, curPath);
        printf("thread done! tid = %lu\n", pthread_self());
    }
}
int handleEvent(int netFd, char* curPath){
    while(1){
        puts("handleEvent");
        int dataLength;
        char commandLine[1024] = {0};
        int ret = recv(netFd, &dataLength, sizeof(dataLength), MSG_WAITALL);
        if(ret == 0){
            break;
        }
        recv(netFd, commandLine, dataLength, MSG_WAITALL);
        printf("length = %d, line = %s\n", dataLength, commandLine);
        // char arr[200] = "";
        // strcpy(arr, commandLine);
        // char *command = strtok(arr, " ");
        char command[100] = {0};
        getCommand(commandLine, command);
        printf("%s\n", command);
        if(strcmp(command, "exit") == 0){
            close(netFd);
            return 0;
        }
        else if(strcmp(command, "cd") == 0){
            //syslog(LOG_INFO, "cd");
            cd(commandLine, curPath, netFd);
        }
        else if(strcmp(command, "ls") == 0){
            //syslog(LOG_INFO, "ls");
            ls(curPath, netFd);
        }
        else if(strcmp(command, "puts") == 0){
            //syslog(LOG_INFO, "puts");
            puts("if puts");
            upload(commandLine, curPath, netFd);
        }
        else if(strcmp(command, "gets") == 0){
            syslog(LOG_INFO, "gets");
            download(commandLine, curPath, netFd);
        }
        else if(strcmp(command, "remove") == 0){
            //syslog(LOG_INFO, "remove");
            rm(commandLine, curPath, netFd);
        }
        else if(strcmp(command, "pwd") == 0){
            //syslog(LOG_INFO, "pwd");
            pwd(curPath, netFd);
        }
        else if(strcmp(command, "mkdir") == 0){
            //syslog(LOG_INFO, "mkdir");
            makedir(commandLine, curPath, netFd);
        }
        else if(strcmp(command, "rmdir") == 0){
            //syslog(LOG_INFO, "rmdir");
            rm(commandLine, curPath, netFd);
        }
        syslog(LOG_INFO, "%s", commandLine);
        train_t t;
        sprintf(t.buf, "%s$", curPath);
        t.dataLength = strlen(t.buf);
        printf("size = %d, bashPre = %s\n", t.dataLength, t.buf);
        send(netFd, &t, sizeof(t.dataLength) + t.dataLength, MSG_NOSIGNAL);
    }    

    return 0;
}